import 'dart:io';

import 'package:flutter/foundation.dart';
import 'package:flutter/gestures.dart';
import 'package:flutter/material.dart';
import 'package:flutter/rendering.dart';
import 'package:flutter/services.dart';
import 'dart:io';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';

class SudGameViewPro extends StatefulWidget {
  const SudGameViewPro({
    super.key,
    this.onPlatformViewCreated,
    this.visible = true,
  });

  /// 平台 View 创建完成回调
  final ValueChanged<int>? onPlatformViewCreated;

  /// 是否可见，false 时隐藏但不销毁
  final bool visible;

  @override
  State<SudGameViewPro> createState() => _SudGameViewProState();
}

class _SudGameViewProState extends State<SudGameViewPro> with AutomaticKeepAliveClientMixin {
  int? _viewId;

  @override
  bool get wantKeepAlive => true;

  @override
  Widget build(BuildContext context) {
    super.build(context);

    final child = Platform.isIOS
        ? _buildIOS()
        : Platform.isAndroid
            ? _buildAndroid()
            : const SizedBox.shrink();

    // 用 Offstage 隐藏，而不是重建销毁
    return Offstage(offstage: !widget.visible, child: child);
  }

  Widget _buildIOS() {
    return UiKitView(
      viewType: 'SudMGPPluginView',
      onPlatformViewCreated: (id) {
        _viewId = id;
        widget.onPlatformViewCreated?.call(id);
      },
    );
  }

  Widget _buildAndroid() {
    // 注意：不加 UniqueKey()，让 AndroidView 能被复用
    return AndroidView(
      viewType: 'SudMGPPluginView',
      onPlatformViewCreated: (id) {
        _viewId = id;
        widget.onPlatformViewCreated?.call(id);
      },
      // 如果有初始化参数，可以加 creationParams
      // creationParams: const <String, dynamic>{},
      // creationParamsCodec: const StandardMessageCodec(),
    );
  }
}

class SudGameView extends StatelessWidget {
  const SudGameView({super.key});

  @override
  Widget build(BuildContext context) {
    if (Platform.isIOS) {
      return UiKitView(
        key: UniqueKey(),
        viewType: 'SudMGPPluginView',
        onPlatformViewCreated: (int viewID) {},
      );
    } else if (Platform.isAndroid) {
      const Map<String, dynamic> creationParams = <String, dynamic>{};
      // return PlatformViewLink(
      //   viewType: 'SudMGPPluginView',
      //   surfaceFactory: (context, controller) {
      //     return AndroidViewSurface(
      //       controller: controller as AndroidViewController,
      //       gestureRecognizers: const <Factory<OneSequenceGestureRecognizer>>{},
      //       hitTestBehavior: PlatformViewHitTestBehavior.opaque,
      //     );
      //   },
      //   onCreatePlatformView: (params) {
      //     return PlatformViewsService.initExpensiveAndroidView(
      //       id: params.id,
      //       viewType: 'SudMGPPluginView',
      //       layoutDirection: TextDirection.ltr,
      //       creationParams: creationParams,
      //       creationParamsCodec: const StandardMessageCodec(),
      //       onFocus: () {
      //         params.onFocusChanged(true);
      //       },
      //     )
      //       ..addOnPlatformViewCreatedListener(params.onPlatformViewCreated)
      //       ..create();
      //   },
      // );

      return AndroidView(
          key: UniqueKey(),
          viewType: 'SudMGPPluginView',
          onPlatformViewCreated: (int viewID) {
            // onViewCreated(viewID);
          });
    }
    return SizedBox();
  }
}
